package com.cbtService;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cbtService.AndroidSDK.HardwareControler;
import com.example.xiaosuan.qweerewrqwe.R;

public class EnvActivity extends Activity {
	public static final String TAG = "EnvActivity";
	ImageView gas, body;
	Button exit;
	TextView temp_tv, hum_tv, body_tv, gas_tv;
	ProgressBar temp, hum;
	public static int hum_state;
	public static int temp_state;
	int ret, serial_fd = 0;
	private Thread mRecvThread;
	public static byte[] buf = new byte[26];
	public static byte[] buf_write = new byte[26];
	public static byte[] buf1 = new byte[23];
	public static byte[] buf_th = new byte[23];
	public static byte[] buf_body = new byte[23];
	public static byte[] buf_gas = new byte[23];
	public static byte[] buf_Fan = new byte[23];
	private final static int MSG_REFRESH = 1;
	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_REFRESH:
				refreshUI();
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_env);
		HardwareControler._init();
		serial_fd = HardwareControler.openSerialPort("/dev/s3c2410_serial2",
				115200, 8, 1);
		init();
	}

	private void init() {
		// ��ʼ����ʪ����ʾ�����
		temp_tv = (TextView) findViewById(R.id.temp_tv);
		hum_tv = (TextView) findViewById(R.id.hum_tv);
		hum = (ProgressBar) findViewById(R.id.pBar_hum);
		temp = (ProgressBar) findViewById(R.id.pBar_temp);
		temp.setProgress(28);
		temp.setMax(35);
		hum.setProgress(16);
		hum.setMax(80);

		gas = (ImageView) findViewById(R.id.gas_iv);
		body = (ImageView) findViewById(R.id.body_iv);
		body_tv = (TextView) findViewById(R.id.tv_body);
		gas_tv = (TextView) findViewById(R.id.tv_gas);
		exit = (Button) findViewById(R.id.button1);
		exit.setOnClickListener(mClick);
	}

	private OnClickListener mClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			EnvActivity.this.finish();
		}
	};

	public void getData() {
		mRecvThread = new Thread(new Runnable() {
			@Override
			public void run() {
				Log.d(this.toString(), Thread.currentThread().getName()
						+ "---->" + "-------------===��ȡ�ڵ���Ϣ");
				Looper.prepare();
				while (!Thread.interrupted()) {
					ret = HardwareControler.select(serial_fd, 2, 5);
					if (ret == 1) {
						HardwareControler.read(serial_fd, buf, 1);
						if ((buf[0] & 0xFF) == 0xEE) {
							 HardwareControler.read(serial_fd, buf, 1);
							if ((buf[0] & 0xFF) == 0xCC) {
								HardwareControler.read(serial_fd, buf, 1);
								if ((buf[0] & 0xFF) == 0x00) {
									HardwareControler.read(serial_fd, buf1, 23);
									if ((buf1[22] & 0xFF) == 0xFF) {
										switch (buf1[11]) {
										case 0x0B:
											buf_gas = buf1;
											//System.out.printf(
											//		"buf_gas---------------->",
											//		buf_gas[19] & 0xFF);
											break;
										case 0x09:
											buf_th = buf1;
											hum_state = (int) (((buf_th[16] & 0xFF) << 8) | (buf_th[17] & 0xFF));
											temp_state = (int) (((buf_th[18] & 0xFF) << 8) | (buf_th[19] & 0xFF));
											break;
										case 0x07:
											buf_body = buf1;
											//for (int i = 0; i < buf_body.length; i++) {
											//	System.out
											//			.printf("buf_body[%d]---------------->%X\n",
											//					i,
											//					buf_body[i] & 0xFF);
											//}
											break;
										case 0x12:
											buf_Fan = buf1;
											//for (int i = 0; i < buf_Fan.length; i++) {
											//	System.out
											//			.printf("buf_Fan[%d]---------------->%X\n",
											//					i,
											//					buf_Fan[i] & 0xFF);
											//}
											buf_write[0] = (byte) 0xEE;
											buf_write[1] = (byte) 0xCC;
											buf_write[2] = (byte) 0x00;
											//for(int i=0;i<buf_Fan.length;i++){
												//buf_write[i+3]=buf_Fan[i];
											//}
											System.arraycopy(buf_Fan, 0, buf_write, 3, 23);
											//for (int i = 0; i < buf_write.length; i++) {
											//	System.out
											//			.printf("buf_write[%d]---------------->%X\n",
											//					i,
											//					buf_write[i] & 0xFF);
											//}
											break;

										}// eof-switch
										Message msg = Message.obtain();
										msg.what = MSG_REFRESH;
										mHandler.sendMessage(msg);
									}
								}// eof-buf[2]
							}// eof-buf[1]
						}// eof-buf[0]

					} else if (ret == 0) {
						System.out.println("û�н��յ���������");
						Log.i("Java Handler", "timeout");
					} else {
						System.out.println("���յ��������ݳ���");
						Log.i("Java Handler", "read error");
					}
				}// eof-while
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Looper.loop();
			}
		});
		mRecvThread.setDaemon(true);
		mRecvThread.start();
	}// eof-getData

	private void refreshUI() {
		if (buf_th != null && buf_th[11] == 0x09) {
			temp_tv.setText(String.valueOf((temp_state / 10.0) + "��"));
			hum_tv.setText(String.valueOf((hum_state / 10.0) + "%"));
			temp.setProgress((temp_state / 10) - 10);
			hum.setProgress((hum_state / 10) - 10);
		}
		if (buf_body != null && buf_body[11] == 0x07) {
			switch (buf_body[19]) {
			case (byte) 0x00:
				body.setBackgroundResource(R.drawable.nousers);
				body_tv.setText("����");
				buf_write[22] = 0x00;
				HardwareControler.write(serial_fd, buf_write);
				break;
			case (byte) 0x01:
				body.setBackgroundResource(R.drawable.haveuser);
				body_tv.setText("����");
				buf_write[22] = 0x01;				
				HardwareControler.write(serial_fd, buf_write);
				break;
			}
		}
		if (buf_gas != null && buf_gas[11] == 0x0B) {
			switch (buf_gas[19]) {
			case (byte) 0x00:
				gas_tv.setText("����");
				gas.setBackgroundResource(R.drawable.burnoff);
				break;
			case (byte) 0x01:
				gas_tv.setText("����");
				gas.setBackgroundResource(R.drawable.burn);
				break;
			}
		}
		if(buf_Fan !=null && buf_Fan[11] == 0x12){
			
		}
		String[] args={new String(""+temp_state/10.0),""+hum_state/10.0,buf_red[19]+"",buf_gas+""};
				sendDB(args);

	}

	private void sendDB(String[] args ) {
		String url="";
		url=url+"gas="+args[3]+"&temperature="+args[0]+"&humidity="+args[1]+"&infrar"
	}

	@Override
	protected void onDestroy() {
		Log.d(TAG, "onDestroy");
		EnvActivity.this.finish();
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		Log.d(TAG, "onPause");
		if (mRecvThread != null) {
			mRecvThread.interrupt();
		}
		super.onPause();
	}

	@Override
	protected void onRestart() {
		Log.d(TAG, "onRestart");
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		Log.d(TAG, "onResume");
		getData();
		super.onResume();
	}

	@Override
	protected void onStart() {
		Log.d(TAG, "onStart");
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.d(TAG, "onStop");
		// TODO Auto-generated method stub
		super.onStop();
	}
}
